package com.xunfang.shareaddress;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.xunfang.shareaddress.method.Communicates;
import com.xunfang.shareaddress.util.GpsConfigFile;

/**
 * LoginActivity由于显示登录界面
 * 
 * @author lizhi
 * 
 */
public class LoginActivity extends Activity {

	// 是泪花组件
	private EditText une, pwd;
	private Button dl, zc;
	private int SET_Address = 1;
	public static boolean flag = false;
	Intent intent_address;

	// 创建菜单栏

	public boolean onCreateOptionsMenu(Menu menu) {
		// TODO Auto-generated method stub
		// 增加菜单项
		menu.addSubMenu(0, SET_Address, 1, "设置");
		return super.onCreateOptionsMenu(menu);
	}

	// 设置菜单响应事件

	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId() == SET_Address) {
			// 启动服务器地址输入界面
			intent_address = new Intent(this, SetServiceAddress.class);
			startActivity(intent_address);
		}
		return super.onOptionsItemSelected(item);
	}

	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.loginlayout);
		// 实例化组件
		une = (EditText) findViewById(R.id.une);
		pwd = (EditText) findViewById(R.id.pwd);
		dl = (Button) findViewById(R.id.dl);
		zc = (Button) findViewById(R.id.zc);
		// 设置监听事件
		dl.setOnClickListener(new OnButtonClick());
		zc.setOnClickListener(new OnButtonClick());
		// 当服务器地址为空时设置服务器地址
		if (GpsConfigFile.ServerAddress == null) {
			// 启动服务器地址设置界面
			intent_address = new Intent(this, SetServiceAddress.class);
			startActivity(intent_address);
		}
	}

	class OnButtonClick implements OnClickListener {

		public void onClick(View v) {
			// TODO Auto-generated method stub
			switch (v.getId()) {
			case R.id.dl:
				// 创建与服务器通信类对象
				Communicates communicates = new Communicates();
				// 验证用户名和密码
				flag = communicates.lCheck(une.getText().toString(), pwd
						.getText().toString());
				System.out.println("标识符---" + flag);
				if (flag) {
					Intent intent = new Intent(LoginActivity.this,
							MainActivity.class);
					intent.putExtra("username", une.getText().toString());
					startActivity(intent);
				} else {
					Toast.makeText(LoginActivity.this, "帐号或密码错误！", 1).show();
				}
				break;
			case R.id.zc:
				// 启动注册界面
				Intent intentz = new Intent(LoginActivity.this,
						RegisteredActivity.class);
				startActivity(intentz);
				break;
			default:
				break;
			}
		}

	}
}
